package com.cht.commonUtil.quartz.chkin;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cht.commonUtil.enums.ChangeType;
import com.cht.commonUtil.enums.MailType;
import com.cht.commonUtil.enums.QuartzJobStatus;
import com.cht.commonUtil.enums.RecipientType;
import com.cht.commonUtil.enums.Roles;
import com.cht.commonUtil.enums.ToDoType;
import com.cht.commonUtil.structure.ResultPack;
import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;
import com.google.gson.Gson;

public class ApplyUpdateQuartzChkIn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding( SP.UIcodeSet );
		response.setCharacterEncoding( SP.UIcodeSet );
		response.setContentType("application/json");
		
		
		int quartzJobTypeId = Integer.parseInt( request.getParameter("quartzJobTypeId") );
		int originalQuartzJobId = Integer.parseInt( request.getParameter("originalQuartzJobId") );
		String cronString = request.getParameter("cronString");
		String empId = request.getParameter("empId");
		String subsysId = request.getParameter("subsysId");
		String subsysDesc = request.getParameter("subsysDesc");
		String empName = request.getParameter("empName");
		String svnUrl = request.getParameter("svnUrl");
		String svnPath = request.getParameter("svnPath");
		
		String sql;
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultPack rp = new ResultPack(); 
		
		try {
			conn = Widgets.getConn();
			
			sql = "INSERT INTO quartzjob (quartzJobTypeId,cronString,creator,cDT,subsysId,status,originalQuartzJobId) VALUES (?,?,?,?,?,1,?)";
			ps = conn.prepareStatement(sql, new String[]{"quartzJobId"} );
			ps.setInt(1, quartzJobTypeId);
			ps.setString(2, cronString);
			ps.setString(3, empId);
			ps.setTimestamp(4, Widgets.cDT());
			ps.setString(5, subsysId);
			ps.setInt(6,originalQuartzJobId);
			ps.execute();
			rs = ps.getGeneratedKeys();
			rs.next();
			final int QUARTZ_JOB_ID = rs.getInt(1);
			if ( ps !=null ) {ps.close();}
			if ( rs !=null ) { rs.close();}			

			//產生待辦事項
			Widgets.genToDoList(subsysId, String.valueOf(QUARTZ_JOB_ID), empId, ToDoType.UPDATE_QUARTZ_CHKIN, Roles.MGR4PROGRAMMER);
			

			String msgBody = "<font face='微軟正黑體'>您好,<p>"+subsysDesc+"的&nbsp;"+empName+"&nbsp;提出了修改排程申請-繳交原始碼的申請!<p> ";	//信件內容
			
			msgBody += "原始碼來源："+svnUrl+svnPath+"/<br>";
			
			//建立LOG，第一步就開始記錄程式的執行
			sql="INSERT INTO changelog (changeLogId,changeTypeId,creatorId,bDate) VALUES (null,?,?,?)";
			ps = conn.prepareStatement(sql, new String[]{"changeLogId"} );
			ps.setInt(1, ChangeType.APPLY_QUARTZ_CHKIN );
			ps.setString(2, empId);
			ps.setTimestamp(3, Widgets.cDT());
			ps.execute();
			rs = ps.getGeneratedKeys();
			rs.next();
			final int QUARTZ_CHECK_IN_CHANGE_LOG_ID = rs.getInt(1);
			if ( ps !=null ) {ps.close();}
			if ( rs !=null ) { rs.close();}
			
			
			msgBody += "<p><p>Best regards<br>"+empName+"</font><p><p>";
			
			//將這筆變更申請目前的進度給修改
			Widgets.updateQuartzStatus(QUARTZ_JOB_ID, QuartzJobStatus.APPROVING);
			
			//完成這次的異動
			sql = "UPDATE changelog SET eDate=? WHERE changeLogId=?";
			ps = conn.prepareStatement(sql);
			ps.setTimestamp(1, Widgets.cDT());
			ps.setInt(2, QUARTZ_CHECK_IN_CHANGE_LOG_ID );
			ps.execute();
			
			//寄發通知信給AP主管
			//信件主旨
			List<String[]> tos = Widgets.getRecipient( MailType.APPLY_QUARTZ_CHKIN,RecipientType.TO,subsysId );
			String[] toppings = {"Arthur","arthymoment@cht.com.tw"};
			tos.add( toppings );
			List<String[]> ccs = Widgets.getRecipient( MailType.APPLY_QUARTZ_CHKIN,RecipientType.CC,subsysId );
			List<String[]> bccs = Widgets.getRecipient( MailType.APPLY_QUARTZ_CHKIN,RecipientType.BCC,subsysId );
			String subject = subsysDesc+"-申請修改排程交付程式碼-排程代碼："+QUARTZ_JOB_ID;
			Widgets.sendMailMain(tos,ccs,bccs,subject, msgBody);
			rp.setSuccess(true);
		} catch(Exception e) {
			rp = Widgets.setResultPack(rp, e);
			Widgets.errLog(e);
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e) { e.printStackTrace(); }
		}
		    
		response.getWriter().write( new Gson().toJson(rp) );
	}
}
